package com.huluobo.a2207douyindemo.login;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2207douyindemo.MainActivity;
import com.huluobo.a2207douyindemo.R;
import com.huluobo.a2207douyindemo.databinding.ActivityLoginBinding;
import com.huluobo.a2207douyindemo.register.RegisterActivity;
import com.huluobo.base.bean.Money;
import com.huluobo.base.db.AppDBUtils;
import com.huluobo.base.mvvm.BaseMVVMActivity;
import com.huluobo.base.net.Const;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class LoginActivity extends BaseMVVMActivity<ActivityLoginBinding, LoginViewModel> {

    @Override
    protected void initData() {
        vm.loginSuccess.observe(this, userBeanResult -> {
            if (userBeanResult.getCode() == 200) {
                ToastUtils.showLong("欢迎回来:" + userBeanResult.getData().getUsername());
                SPUtils.getInstance().put(Const.PARAM_TOKEN, userBeanResult.getData().getToken());
                SPUtils.getInstance().put(Const.PARAM_USERNAME, userBeanResult.getData().getUsername());
                Money money = new Money(10000F, userBeanResult.getData().getUsername());
                AppDBUtils.getInstance().getMoneyDao().insertMoney(money);
                AppDBUtils.getInstance().getUserDao().insertUserInfo(userBeanResult.getData());
                //登录环信服务器
                EMClient.getInstance().login(userBeanResult.getData().getUsername(),
                        userBeanResult.getData().getPassword(), new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            }

                            @Override
                            public void onError(int code, String error) {
                                Log.i("TagA", "登录环信失败:" + error + ",code:" + code);
                            }
                        });
            } else {
                binding.loginUsername.setError("登录失败:" + userBeanResult.getMessage());
            }
        });

        vm.loginFailed.observe(this, s -> {
            Log.i("TagA", "登录失败:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.loginBtn.setOnClickListener(v -> {
            if (binding.loginUsername.getText().toString().isEmpty() || binding.loginPassword.getText().toString().isEmpty()) {
                binding.loginUsername.setError("用户名密码不能为空");
            } else {
                vm.login(
                        binding.loginUsername.getText().toString(),
                        binding.loginPassword.getText().toString()
                );

            }
        });
        binding.loginHeader.headerBack.setVisibility(View.GONE);
        binding.loginHeader.headerTitle.setText("登录");

        binding.loginRegister.setOnClickListener(v -> {
            startActivity(new Intent(this, RegisterActivity.class));
        });
    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}